package com.tcwlkj.uuService.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.MyAccountModel;
import com.tcwlkj.uuService.utils.ChcheUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/10/25 0025.
 */

public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private MyAccountModel mMsgList;
    //private MyAccountModel AccountModel;
    private List<MyAccountModel.Rows> mRowsList=new ArrayList<>();

    public AccountAdapter(MyAccountModel mMsgList) {
        this.mMsgList = mMsgList;
    }
    public void updateData(MyAccountModel data,List<MyAccountModel.Rows> rowses) {
        this.mMsgList = data;
        this.mRowsList=rowses;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new AccountAdapter.ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent, false));
        } else  {
            return new AccountAdapter.ViewHolders(View.inflate(parent.getContext(), R.layout.item_account, null));
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolders) {
            ((ViewHolders) holder).SubjectName.setText(mRowsList.get(position-1).getRemarks());
            ((ViewHolders) holder).tv_time.setText(mRowsList.get(position-1).getAddTime());
            if (mRowsList.get(position-1).getInOut() == 1) {
                ((ViewHolders) holder).plusmoney.setText("+" + mRowsList.get(position-1).getRealMoney());
            } else if (mRowsList.get(position-1).getInOut() == -1) {
                ((ViewHolders) holder).plusmoney.setText("-" + mRowsList.get(position-1).getRealMoney());
            } else if (mRowsList.get(position-1).getInOut() == 0) {
                ((ViewHolders) holder).plusmoney.setText(mRowsList.get(position-1).getRealMoney());
            }
            ((ViewHolders) holder).user_yu_e.setText(mRowsList.get(position-1).getAfterMoney());
        } else if (holder instanceof ItemViewHolder) {
            if(ChcheUtils.getInstance().getDriverDetailModel()!=null){
                ((ItemViewHolder) holder).yu_e_tv.setText(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance());
                if (Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance()) > 200) {
                    ((ItemViewHolder) holder).ke_ti_xain_tv.setText(Math.floor((Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance()) - 200) / 100)* 100 + "");
                } else {
                    ((ItemViewHolder) holder).ke_ti_xain_tv.setText("0.00");
                }
            }
            ((ItemViewHolder) holder).today_sum_money.setText(mMsgList.getNowIncome());
            ((ItemViewHolder) holder).lj_money.setText(mMsgList.getTotalIncome());
        }
    }
    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return 0;
        }else {
            return 1;
        }
        //return super.getItemViewType(position);
    }

    //    @Override
//    public void onBindViewHolder( ViewHolder holder, int position) {
//        //AccountModel=mMsgList.get(position);
//
//        (ViewHolders(holder)).SubjectName.setText(mMsgList.getRows().get(position).getRemarks());
//        holder.tv_time.setText(mMsgList.getRows().get(position).getAddTime());
//        if(mMsgList.getRows().get(position).getInOut()==1){
//            holder.plusmoney.setText("+"+mMsgList.getRows().get(position).getMoney());
//        }else if(mMsgList.getRows().get(position).getInOut()==-1) {
//            holder.plusmoney.setText("-"+mMsgList.getRows().get(position).getMoney());
//        }else if(mMsgList.getRows().get(position).getInOut()==0){
//            holder.plusmoney.setText(mMsgList.getRows().get(position).getMoney());
//        }
//        holder.user_yu_e.setText(mMsgList.getRows().get(position).getAfterMoney());
//
//    }
    @Override
    public int getItemCount() {
        return mRowsList.size() + 1;
    }

    public static class ViewHolders extends RecyclerView.ViewHolder {
        TextView SubjectName;
        TextView tv_time;
        TextView user_yu_e;
        TextView plusmoney;

        public ViewHolders(View itemView) {
            super(itemView);
            SubjectName = (TextView) itemView.findViewById(R.id.SubjectName);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            user_yu_e = (TextView) itemView.findViewById(R.id.user_yu_e);
            plusmoney = (TextView) itemView.findViewById(R.id.plusmoney);
        }
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView yu_e_tv;
        TextView ke_ti_xain_tv;
        TextView today_sum_money;
        TextView lj_money;

        public ItemViewHolder(View itemView) {
            super(itemView);
            yu_e_tv = (TextView) itemView.findViewById(R.id.yu_e_tv);
            ke_ti_xain_tv = (TextView) itemView.findViewById(R.id.ke_ti_xain_tv);
            today_sum_money = (TextView) itemView.findViewById(R.id.today_sum_money);
            lj_money = (TextView) itemView.findViewById(R.id.lj_money);
        }
    }
}
